const { getPublicPath, getAssetPath, getConfig, getPackageJson } = require('./util')
const webpack = require('webpack');
function loaders(module) {
}


const defaultChainWebpackConfig = (config) => {
    // if (getConfig().sentryDsn) {
    //     config.plugin('SentryCliPlugin')
    //         .use(SentryCliPlugin, [{
    //             include: `./output/${getAssetPath()}/js`,
    //             release: getConfig()?.appVersion,
    //             org: process.env.VUE_APP_SENTRY_ORG,
    //             project: process.env.VUE_APP_SENTRY_PROJECT,
    //             authToken: process.env.VUE_APP_SENTRY_AUTH,
    //             url: process.env.VUE_APP_SENTRY_URL,
    //             cleanArtifacts: true, // 先清理再上传
    //             ignore: ['node_modules', 'vue.config.js'],
    //             urlPrefix: getPublicPath() + `${getAssetPath}/js`,
    //             errorHandler: (err, invokeErr, compilation) => { compilation.warnings.push('Sentry CLI Plugin: ' + err.message) }
    //         }])
    // }
    config.plugin('html').tap((args) => {
        args[0].title = getConfig().title;
        args[0].baseUrl = getConfig().cdnUrl;
        args[0].static = getAssetPath();
        args[0].name = getPackageJson().name;
        return args;
    });
    config.plugin('banner')
        .use(
            new webpack.BannerPlugin({
                banner(args) {
                    return '@charset "UTF-8";\n';
                },
                raw: true,
                test: [/\.css$/i],
                exclude: /vender/i,
            })
        );

    if (process.env.NODE_ENV === 'production') {
        config.performance
            // .hints(false)
            .maxEntrypointSize(50000000)
            .maxAssetSize(30000000)
            .assetFilter((assetFilename) => {
                return assetFilename.endsWith('.js');
            });
        config.plugin('extract-css').tap(() => [
            {
                filename: `${getAssetPath()}/css/[name].[fullhash:8].css`,
                chunkFilename: `${getAssetPath()}/css/[name].[contenthash:8].chunk.css`,
                ignoreOrder: true
            }
        ]);
        config.output
            .filename(`${getAssetPath()}/js/[name].[fullhash:8].js`)
            .chunkFilename(`${getAssetPath()}/js/[name].[contenthash:8].chunk.js`);
    }
    loaders(config.module);

}

module.exports = {
    defaultChainWebpackConfig
}